package com.why.whyojcodesandbox.controller;

import com.why.whyojcodesandbox.JavaDockerCodeSadBox;
import com.why.whyojcodesandbox.model.dto.ExecuteCodeRequest;
import com.why.whyojcodesandbox.model.dto.ExecuteCodeResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController("/")
public class MainContrller {
    private static String AUTH_REQUEST_HEADER = "secretHeader";
    private static String AUTH_REQUEST_SECRET = "asjdlkfjlaksalsfkj123123jlkj";
    @Resource
    private JavaDockerCodeSadBox javaDockerCodeSadBox;
    @PostMapping("executeCode")
    public ExecuteCodeResponse executeCode(@RequestBody ExecuteCodeRequest executeCodeRequest, HttpServletRequest request, HttpServletResponse response) {
        String header = request.getHeader(AUTH_REQUEST_HEADER);
        if (! (header.equals(AUTH_REQUEST_SECRET))) {
            response.setStatus(403);
            return null;
        }
        ExecuteCodeResponse executeCodeResponse = javaDockerCodeSadBox.executeCode(executeCodeRequest);
        return executeCodeResponse;
    }

    @GetMapping("nice")
    public String test() {
        System.out.println("okokokokok");
        return "ok";
    }
}
